package com.ipfsemc.ipfsemc.service.imp;

import com.ipfsemc.ipfsemc.mapper.ProductMapper;
import com.ipfsemc.ipfsemc.model.Product;
import com.ipfsemc.ipfsemc.service.IProductService;
import com.ipfsemc.ipfsemc.util.CodecUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    ProductMapper productMapper;


    /**
     * 首页产品全部显示
     * @param product
     * @return
     */
    @Override
    public List<Product> list(Product product) {
        return productMapper.list(product);
    }

    @Override
    public List<Product> listProduct() {
        return productMapper.listProduct();
    }

    /**
     * 点击购买显示产品详情根据产品类别ID
     * @param proId
     * @return
     */
    @Override
    public Product selectProByKey(String proId) {
        return productMapper.selectProByKey(proId);
    }

    @Override
    public int add(Product product) {
        product.setProId(CodecUtil.createId());
        int i = productMapper.insertCategory(product);
        int j = productMapper.insertSelective(product);

        if (i>0&&j>0){
            return 1;
        }
        return -1;
    }

    @Override
    public int update(Product product) {
        int i = productMapper.updateCategory(product);
        int j = productMapper.updateByPrimaryKeySelective(product);

        if (i>0&&j>0){
            return 1;
        }
        return -1;
    }

    @Override
    public int del(String proId) {
        return productMapper.deleteByPrimaryKey(proId);
    }
}
